const redis = require('../../config/redis');
const appKey = require('../../config/appKey');
class DriversReport {
    /**
     * 接收机器状态上报
     * @api {POST}        /api/ReportStatus                接收机器状态上报
     * @apiName           /ReportStatus
     * @apiGroup          ReportStatus
     * @apiVersion 0.1.0
     * @apiParam          {String}          apiKey         api秘钥
     * @apiParam          {String}          DevCode        设备编号
     * @apiParam          {Int}             IsOnline=0     是否在线
     * @apiParam          {Int}             UseState=0     状态
     * @apiSuccessExample Success-Response:
     * {
        "result": true,
        “messages”: "请求成功",
       }
     */
    async ReportStatus(ctx) {
        const {
            db,
            request: {
                query: { apiKey, DevCode, IsOnline, UseState }
            }
        } = ctx;

        if (!apiKey) {
            ctx.errors('apiKey不能为空');
            return;
        }
        if (!DevCode) {
            ctx.errors('DevCode不能为空');
            return;
        }
        if (!IsOnline) {
            ctx.errors('IsOnline不能为空');
            return;
        }
        if (!UseState) {
            ctx.errors('UseState不能为空');
            return;
        }
        if (apiKey !== appKey.appKey) {
            ctx.errors('秘钥错误');
            return;
        }

        let data = await db.Drives.findOne({ where: { deviceId: DevCode } });
        data.isOnline = IsOnline;
        data.useState = UseState;
        data.save();
        ctx.set('Content-Type', 'application/json');
        ctx.body = { result: true, messages: '请求成功' };
    }
    /**
     * 接收订单机器启动通知(开始洗车)
     * @api {POST}        /api/BeginService                 接收订单机器启动通知(开始洗车)
     * @apiName           /BeginService
     * @apiGroup          BeginService
     * @apiVersion 0.1.0
     * @apiParam          {String}          apiKey         api秘钥
     * @apiParam          {String}          DevCode        设备编号
     * @apiParam          {String}          OrderNo        OrderNo
     * @apiSuccessExample Success-Response:
     * {
        "result": true,
        “messages”: "请求成功",
       }
     */
    async BeginService(ctx) {
        const {
            db,
            request: {
                query: { apiKey, DevCode, OrderNo }
            }
        } = ctx;

        if (!apiKey) {
            ctx.errors('apiKey不能为空');
            return;
        }
        if (!DevCode) {
            ctx.errors('DevCode不能为空');
            return;
        }
        if (!OrderNo) {
            ctx.errors('OrderNo不能为空');
            return;
        }
        if (apiKey !== appKey.appKey) {
            ctx.errors('秘钥错误');
            return;
        }

        let data = await db.Drives.findOne({ where: { deviceId: DevCode } });
        data.isOnline = 1;
        data.useState = 3;
        data.save();
        ctx.set('Content-Type', 'application/json');
        ctx.body = { result: true, messages: '请求成功' };
    }
    /**
     * 可退费订单退费，洗车结束
     * @api {POST}        /api/FinishService                 可退费订单退费，洗车结束
     * @apiName           /FinishService
     * @apiGroup          FinishService
     * @apiVersion 0.1.0
     * @apiParam          {String}          apiKey          api秘钥
     * @apiParam          {String}          DevCode         设备编号
     * @apiParam          {String}          OrderNo         OrderNo
     * @apiParam          {Int}             Fee             Fee
     * @apiParam          {String}          OverTime        OverTime
     * @apiSuccessExample Success-Response:
     * {
        "result": true,
        “messages”: "请求成功",
       }
     */
    async FinishService(ctx) {
        const {
            db,
            request: {
                query: { apiKey, DevCode, OrderNo, Fee, OverTime }
            }
        } = ctx;

        if (!apiKey) {
            ctx.errors('apiKey不能为空');
            return;
        }
        if (!DevCode) {
            ctx.errors('DevCode不能为空');
            return;
        }
        if (!OrderNo) {
            ctx.errors('OrderNo不能为空');
            return;
        }
        if (!Fee) {
            ctx.errors('Fee不能为空');
            return;
        }
        if (!OverTime) {
            ctx.errors('OverTime不能为空');
            return;
        }
        if (apiKey !== appKey.appKey) {
            ctx.errors('秘钥错误');
            return;
        }

        // 更新订单状态
        let data = await db.Use.findOne({ where: { orderID: OrderNo } });
        data.status = 1;
        data.save();
        // 删除redis状态
        let user = await db.User.findOne({
            where: { id: data.userId },
            attributes: ['openId']
        });
        const key = user.openId + DevCode;
        redis.del(key);
        ctx.set('Content-Type', 'application/json');
        ctx.body = { result: true, messages: '请求成功' };
    }
}
module.exports = new DriversReport();
